<?php
class Card extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function uploadDetails() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			if ($this->input->post ()) {
				$this->load->model ( 'Mcard' );
				
				$config ['upload_path'] = './uploads/business/';
				$config ['allowed_types'] = 'xls|xlsx|csv';
				$config ['overwrite'] = true;
				$this->load->library ( 'upload', $config );
				
				if (! $this->upload->do_upload ( 'userfile' )) {
					$error ['msg'] = $this->upload->display_errors ();
				} else {
					$data = $this->upload->data ();
					$filePath = './uploads/business/' . $data ['file_name'];
					
					require_once 'application/libraries/PHPExcel.php';
					require_once 'application/libraries/PHPExcel\IOFactory.php';
					require_once 'application/libraries/PHPExcel\Reader\Excel5.php';
					require_once 'application/libraries/PHPExcel\Reader\CSV.php';
					
					$PHPExcel = new PHPExcel ();
					$PHPReader = new PHPExcel_Reader_Excel2007 ();
					if (! $PHPReader->canRead ( $filePath )) {
						$PHPReader = new PHPExcel_Reader_Excel5 ();
						if (! $PHPReader->canRead ( $filePath )) {
							$PHPReader = new PHPExcel_Reader_CSV ();
							if (! $PHPReader->canRead ( $filePath )) {
								$error ['msg'] = '不能读取文件' . $filePath;
								$error ['url'] = base_url () . 'admin/card/uploadDetails';
								$this->load->view ( 'admin/admin_message', $error );
								return;
							}
						}
					}
					
					$PHPReader->setDelimiter ( ';' );
					$PHPReader->setEnclosure ( '' );
					$PHPReader->setLineEnding ( "\r\n" );
					$PHPExcel = $PHPReader->load ( $filePath );
					$currentSheet = $PHPExcel->getActiveSheet ();
					$allRow = $currentSheet->getHighestRow ();
					if ($currentSheet->getCell ( "A1" )->getValue ()) {
						for($j = 1; $j <= $allRow; $j ++) {
							$str = '';
							for($k = 'A'; $k <= 'E'; $k ++) {
								$str .= $currentSheet->getCell ( "$k$j" )->getCalculatedValue () . '\\';
							}
							$strs = explode ( "\\", $str );
							if ($strs [0] == NULL)
								break;
							$mdata ['cardId'] = $strs [0];
							$mdata ['business'] = $strs [2];
							$mdata ['time'] = $strs [1];
							$mdata ['sum'] = $strs [3];
							$mdata ['balance'] = $strs [4];
							$bid = $this->Mcard->addDetails ( $mdata );
							if (! $bid) {
								$error ['msg'] = '数据库写入失败！';
								$error ['url'] = base_url () . 'admin/business/uploadDetails';
								$this->load->view ( 'admin/admin_message', $error );
								return;
							}
						}
						$error ['msg'] = '导入成功！';
					} else
						$error ['msg'] = 'Excel模板错误，请检查是否选择了正确的文件。';
				}
				$error ['url'] = base_url () . 'admin/card/uploadDetails';
				$this->load->view ( 'admin/admin_message', $error );
			} else {
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/card_uploadDetails' );
				$this->load->view ( 'admin/footer' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有导入特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function uploadBalance() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			if ($this->input->post ()) {
				$this->load->model ( 'Mcard' );
				
				$config ['upload_path'] = './uploads/business/';
				$config ['file_name'] = 'balance';
				$config ['allowed_types'] = 'csv';
				$config ['overwrite'] = true;
				$this->load->library ( 'upload', $config );
				
				if (! $this->upload->do_upload ( 'userfile' )) {
					$error ['msg'] = $this->upload->display_errors ();
				} else {
					$data = $this->upload->data ();
					$filePath = $data ['full_path'];
					
					if ($this->Mcard->balanceUpdate ( $filePath )) {
						$error ['msg'] = '导入成功！';
					} else {
						$error ['msg'] = '导入失败！';
					}
					$error ['msg'] = "upload success!";
				}
				$error ['url'] = base_url () . 'admin/card/uploadBalance';
				$this->load->view ( 'admin/admin_message', $error );
			} else {
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/card_uploadBalance' );
				$this->load->view ( 'admin/footer' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有导入特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function appList() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$this->load->model ( 'Mcard' );
			$this->load->library ( 'pagination' );
			
			$data ['search'] = '';
			$search = '';
			if ($this->input->post ( 'search' )) {
				$data ['search'] = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
				if ($data ['search'] != '') {
					$searchRow = explode ( ' ', $data ['search'] );
					$search = '(';
					foreach ( $searchRow as $row ) {
						if ($row) {
							$search .= "companyName like '%" . $row . "%' OR ";
							$search .= "name like '%" . $row . "%' OR ";
							$search .= "phone like '%" . $row . "%' OR ";
						}
					}
					$search = substr ( $search, 0, strlen ( $search ) - 3 );
					if ($search)
						$search .= ')';
				}
			}
			
			$data ['listMode'] = $this->uri->segment ( 4, 0 );
			$data ['page'] = $this->uri->segment ( 5, 0 );
			$data ['num'] = $this->Mcard->getCardAppNum ( $data ['listMode'], $search );
			
			$config ['base_url'] = base_url () . '/admin/card/appList/' . $data ['listMode'] . '/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 5;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Mcard->getCardAppList ( $data ['listMode'], $data ['page'], PAGE_SHOW_NUM, $search );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/card_appList' );
			$this->load->view ( 'admin/footer' );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function dealApp() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mcard' );
			$data ['deal'] = $this->input->post ( 'deal' );
			$data ['state'] = 'T';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['updatePerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mcard->updateCardApp ( $data )) {
				$data ['msg'] = '处理状态更新成功！';
			} else
				$data ['msg'] = '处理状态更新失败！';
			
			$data ['url'] = base_url () . 'admin/card/appList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
	
	function deleApp() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1') || ($this->session->userdata ( 'authority' ) === '2')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mcard' );
			$data ['state'] = 'D';
			$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
			$data ['updatePerson'] = $this->session->userdata ( 'adminName' );
			if ($this->Mcard->updateCardApp ( $data )) {
				$data ['msg'] = '卡片申购删除成功！';
			} else
				$data ['msg'] = '卡片申购删除失败！';
			
			$data ['url'] = base_url () . 'admin/card/appList/2';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			$data ['msg'] = '请先登录！';
			$data ['url'] = base_url () . 'admin/home/login';
			$this->load->view ( 'admin/admin_message', $data );
		}
	}
}